package cn.big.netty.learn.echo.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;

/**
 * Created by LiJianXin on 2019/5/9 0009.
 */
public class EchoClient {
	public static void main(String[] args) throws Exception {
		//EventLoopGroup可以理解为线程池，这个线程池用来处理连接、接收数据、发送数据
		EventLoopGroup loopGroup = new NioEventLoopGroup();
		try {
			Bootstrap bootstrap = new Bootstrap();
			bootstrap.group(loopGroup)
					.channel(NioSocketChannel.class)
					.remoteAddress(new InetSocketAddress("localhost", 8888))
					.handler(new ChannelInitializer<SocketChannel>() {
						@Override
						protected void initChannel(SocketChannel ch) throws Exception {
							ch.pipeline().addLast(new EchoClientHandler());
						}
					});
			ChannelFuture channelFuture = bootstrap.connect().sync();
			channelFuture.channel().closeFuture().sync();
		} finally {
			loopGroup.shutdownGracefully().sync();
		}
	}
}
